package com.e.ego.util

import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.Editor
import android.util.Base64

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream

/**
 * SharedPreferences 数据存储工具类

 * @author lxy
 */
object ESPUtils {

    @JvmField var name = "shared_preferences"

    @JvmStatic fun <T> putObject(context: Context, key: String, t: T): Boolean {
        return putObject(context, name, key, t)
    }

    /**储存复杂的数据字段对象*/
    @JvmStatic fun <T> putObject(context: Context, name: String,
                                 key: String, t: T?): Boolean {
        try {
            val sp = context.getSharedPreferences(name,
                    Context.MODE_PRIVATE)
            // 存储
            val editor = sp.edit()
            if (t == null) {
                editor.putString(key, "")
                editor.commit()
                return true
            }
            val toByte = ByteArrayOutputStream()
            val oos: ObjectOutputStream

            oos = ObjectOutputStream(toByte)
            oos.writeObject(t)
            // 对byte[]进行Base64编码
            val payCityMapBase64 = String(Base64.encode(
                    toByte.toByteArray(), Base64.DEFAULT))

            editor.putString(key, payCityMapBase64)
            editor.commit()
            return true
        } catch (e: IOException) {
            e.printStackTrace()
            return false
        }
    }

    @JvmStatic fun <T> obtainObject(context: Context, key: String): T? = ESPUtils.obtainObject(context, name, key)

    /**得到复杂的数据对象*/
    @JvmStatic fun <T> obtainObject(context: Context, name: String,
                                    key: String): T? {
        try {
            val sp = context.getSharedPreferences(name,
                    Context.MODE_PRIVATE)
            val payCityMapBase64 = sp.getString(key, "")
            if (payCityMapBase64!!.length == 0) {
                return null
            }
            val base64Bytes = Base64
                    .decode(payCityMapBase64, Base64.DEFAULT)
            val bais = ByteArrayInputStream(base64Bytes)
            val ois = ObjectInputStream(bais)
            return ois.readObject() as T
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return null
    }

    /**存储字符串数据类型*/
    @JvmStatic fun putString(context: Context, key: String, value: String) {
        val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.putString(key, value)
        editor.commit()
    }

    @JvmStatic fun obtainString(context: Context, key: String, defValue: String = ""): String {
        val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        return sp.getString(key, defValue)
    }

    @JvmStatic fun putBoolean(context: Context, key: String, value: Boolean) {
        val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.putBoolean(key, value)
        editor.commit()
    }

    @JvmStatic fun obtainBoolean(context: Context, key: String, defValue: Boolean = false): Boolean {
        val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        return sp.getBoolean(key, defValue)
    }

    @JvmStatic fun putInt(context: Context, key: String, value: Int) {
        val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.putInt(key, value)
        editor.commit()
    }

    @JvmStatic fun obtainInt(context: Context, key: String, defaultValue: Int = 0): Int {
        val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        return sp.getInt(key, defaultValue)
    }

    @JvmStatic fun putFloat(context: Context, key: String, value: Float) {
        val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.putFloat(key, value)
        editor.commit()
    }

    @JvmStatic fun obtainFloat(context: Context, key: String): Float {
        val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        return sp.getFloat(key, 0f)
    }

    @JvmStatic fun obtainLong(context: Context, key: String, value: Long) {
        val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        val editor = sp.edit()
        editor.putLong(key, value)
        editor.commit()
    }

    @JvmStatic fun obtainLong(context: Context, key: String): Long {
        val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        return sp.getLong(key, -1)
    }

    @JvmStatic fun preferences(context: Context): SharedPreferences {
        return context.getSharedPreferences(name, Context.MODE_PRIVATE)
    }

    @JvmStatic fun editor(context: Context): Editor {
        return preferences(context).edit()
    }

    @JvmStatic fun clear(context: Context) {
        editor(context).clear().commit()
    }
}
